<template>
	<view>
		<!-- <view style="height: 70upx;">
		</view> -->
		<view :class='["calendar",hideEdge,viewType+"-view"]'>
			<view class='week-label-row' :class="loginType==2?'Vipztrli':''">
				<!-- :class="item == '六'||item == '日'?'colorred':''"  -->
				<view v-for='(item,index) in weekLabel' :key="index">
					{{item}}
				</view>
			</view>
			<swiper circular :duration="200" :data-data="views" :current="currentIndex" @change="swiperChange"
				@animationfinish="swiperFinish" @transitionend="transitionEnd" @touchstart="startTouch"
				@touchmove="moveTouch" @touchend="endTouch" @touchcancel="cancelTouch" :class="animate">
				<block v-for="(item,idx) in views" :key="idx">
					<swiper-item>
						<view @tap="suoxiao" class='grid' :style='"top:"+topValues[idx]+"rpx"'>
							<view v-for="(dayItem,dayIndex) in item" :key='dayIndex'
								@tap='selectDate(dayItem.label,dayItem)' :class="['item item-'+dayItem.type,viewIndex==idx && currentDate==dayItem.date ? 'selected':''
							 ,'item','item-'+dayItem.date==dayItem.jintian? 'sens':'']">
								<view class='inner'
									:class="[indicator[dayItem.date] ? 'indicator indicator-' + themeColors[indicator[dayItem.date]] :'']">
									<view :class="dayItem.ishave == 1?'louChoose':''">
										<view class="dateStyle"
											:class="dayItem.date==startTime.createDate||dayItem.date==endTime.createDate?'dateStyleactave':''">
											{{dayItem.day}}
										</view>
									</view>
									<view v-if="dayItem.ishaveDay==1" class="Optional">
									</view>
								</view>

							</view>
						</view>
					</swiper-item>
				</block>
			</swiper>
		</view>
		<!-- <view class="Unfoldandstow" @tap="openHideTab(viewType)"> -->
		<!-- <view class="Unfoldandstow">
		</view> -->
	</view>
</template>

<script>
	import Calendar from "./calendar.js"
	import WxTouch from "./wx-touch.js"
	import festival from "./festival.js";
	import mif from './mif';

	function sswitch(date) {
		let arrdata = date.split("-")
		let number = parseInt(arrdata[1]),
			number1 = parseInt(arrdata[2]);
		if (number1 <= 9) {
			arrdata[2] = '0' + arrdata[2]
		}
		if (number <= 9) {
			arrdata[1] = '0' + arrdata[1]
			arrdata = arrdata.join('-')
			// arrdata = parseInt(arrdata)
		} else {
			arrdata = arrdata.join('-')
			// arrdata = parseInt(arrdata)
		}
		return arrdata
	}
	// import time from '@/basejs/time';
	export default {
		props: {
			// value: {//解开会报错
			// 	type: String,
			// observer(value, oldValue, path) {
			// 	// 监听 value 值变化
			// 	if (this.calendar.watchValue && value != this.currentDate) {
			// 		this.watchValue(value);
			// 	}
			// }
			// },
			// 指示灯
			canChoose: Array,
			yearAndMonth: String,
			chooseDataLou: Array,
			indicator: {
				type: Object,
				default: Object
			},
			indicator: {
				type: Object,
				default: Object
			},
			startTime: {
				type: Object,
				default: Object
			},
			endTime: {
				type: Object,
				default: Object
			}
			// value: {
			//     "2018-9-8": 1,
			//     "2018-9-9": 2,
			//     "2018-9-10": 1,
			//     "2018-9-11": 1,
			//     "2018-9-12": 2
			// }
		},
		data() {
			return {
				result: [], //接收到的记录数据
				viewType: 'month',
				loginType: 1,
				sessionid: '',
				ltop: 0,
				xiu: [], //休数组
				luckarr: [], //吉数组
				fierce: [], //忌数组
				weekLabel: ["日", "一", "二", "三", "四", "五", "六"],
				views: [], // 视图组
				currentIndex: 0, // 当前索引，控制 swiper
				viewIndex: 0, // 当前索引，不控制 swiper
				hideEdge: "", // 是否隐藏边缘，因为 42 格子的周视图，会漏出相邻日期的激活状态样式
				topValues: [0, 0, 0], // 控制周视图的隐藏高度
				currentDate: "", // 标记今天
				animate: "", // 切换周月视图时开启动画过渡
				sfsowx: "", //是否启动动画
				qujuassd: "",
			}
		},

		watch: {
			//haveTask 为接收参数的名称
			chooseDataLou: {
				handler(item, index) {
					console.log(this.views, '当前是否有数据')
					let _this = this;
					let one = _this.views[0]
					let all = _this.views;
					let obj = {
						"name": "tom",
						"age": 16
					}
					one.forEach(function(v, index) {
						v.date = v.year + '-' + _this.addTimes(v.month) + '-' + _this.addTimes(v.day);
						if (v.ishave == false) {
							let key = 'ishave'
							let value = 3;
							v[key] = value
						} else {
							v.ishave = 3;
						}
						_this.chooseDataLou.forEach(function(a, i) {
							if (v.date == a) {
								v.ishave = 1;
							}
						})
					})
					_this.views[1].forEach(function(v, index) {
						v.date = v.year + '-' + _this.addTimes(v.month) + '-' + _this.addTimes(v.day);
						if (v.ishave == false) {
							let key = 'ishave'
							let value = 3;
							v[key] = value
						} else {
							v.ishave = 3;
						}
						_this.chooseDataLou.forEach(function(a, i) {
							if (v.date == a) {
								v.ishave = 1;
							}
						})
					})
					_this.views[2].forEach(function(v, index) {
						v.date = v.year + '-' + _this.addTimes(v.month) + '-' + _this.addTimes(v.day);

						if (v.ishave == false) {
							let key = 'ishave'
							let value = 3;
							v[key] = value
						} else {
							v.ishave = 3;
						}
						_this.chooseDataLou.forEach(function(a, i) {
							if (v.date == a) {
								v.ishave = 1;
							}
						})
					})
					_this.views = _this.calendar.refreshWeekViews(_this.currentDate).views;
				},
				deep: true // 深度监听父组件传过来对象变化
			},
			canChoose: {
				handler(item, index) {
					if(this.yearAndMonth){
						this.currentDate = this.yearAndMonth +'-1';
					}
					
					console.log(this.canChoose, '你是个撒谎')

					console.log('你大爷变化了')
					this.xiudian()


					// _this.views = _this.calendar.refreshWeekViews(_this.currentDate).views;

				},
				deep: true // 深度监听父组件传过来对象变化
			},
		},

		/**
		 * 初始化控制器
		 */
		created() {
			this.calendar = new Calendar();
			let _this = this;
			this.watchValue(this.value);
			this.calendar.watchValue = true;
		},
		methods: {
			/**
			 * 监听 swiper 改变
			 */
			//格式化时间
			//
			xiudian() {
				let _this = this;
				_this.views = _this.calendar.refreshWeekViews(_this.currentDate).views;
				

				let one = _this.views[0]
				let all = _this.views;
				one.forEach(function(v, index) {
					// v.date = v.year + '-' + _this.addTimes(v.month) + '-' + _this.addTimes(v.day);
					v['ishaveDay'] = 0
					_this.canChoose.forEach(function(a, i) {

						if (v.date == a) {
							v.ishaveDay = 1;
							console.log(22)
						}
					})
				})
				_this.views[1].forEach(function(v, index) {
					// v.date = v.year + '-' + _this.addTimes(v.month) + '-' + _this.addTimes(v.day);
					v['ishaveDay'] = 0
					_this.canChoose.forEach(function(a, i) {
						if (v.date == a) {
							v.ishaveDay = 1;
							console.log(22)
						}
					})
				})
				_this.views[2].forEach(function(v, index) {
					// v.date = v.year + '-' + _this.addTimes(v.month) + '-' + _this.addTimes(v.day);
					v['ishaveDay'] = 0
					_this.canChoose.forEach(function(a, i) {
						if (v.date == a) {
							v.ishaveDay = 1;
							console.log(22)
						}
					})
				})

				console.log(_this.currentDate, '浙江是撒')


				_this.views = _this.calendar.refreshWeekViews(_this.currentDate).views;




				console.log(_this.views, '变化后的大爷')
			},

			addTimes(m) {
				return m < 10 ? '0' + m : m
			},

			openHideTab(e) {
				let thisd = this;
				let setData = {},
					data = thisd,
					trigger = false,
					calendar = thisd.calendar;
				if (e == 'month') {
					thisd.views = calendar.refreshWeekViews(data.currentDate).views;
					thisd.topValues = calendar.refreshWeekViews(data.currentDate).topValues
					data.viewType = "week";
					trigger = true;
				} else {
					thisd.views = calendar.refreshMonthViews(calendar.currentMonth).views
					// setData = calendar.refreshMonthViews(calendar.currentMonth);
					data.viewType = "month";
					thisd.topValues = [0, 0, 0];
					trigger = true;
				}
			},
			swiperChange(evt) {
				let index = evt.detail.current,
					calendar = this.calendar,
					// 根据视图类型使用不同的刷新机制
					data = this.viewType == "week" ?
					calendar.changeWeekViews(index) :
					calendar.changeMonthViews(index);
				// 更新视图索引的标记，仅仅是标记
				this.viewIndex = index;
				// let assignment =  getFirst(data);
				let assignment = Object.entries(data)
				let views = assignment[0],
					topValues = assignment[1],
					viewsdata = this.views,
					topValuesdata = this.topValues;
				// console.log(viewsdata,topValuesdata,topValues[1],123)
				if (views[0] == "views[0]") {
					viewsdata[0] = views[1];
					if (topValues) {
						topValuesdata[0] = topValues[1];
					}
				} else if (views[0] == "views[1]") {
					viewsdata[1] = views[1];
					if (topValues) {
						topValuesdata[1] = topValues[1];
					}
				} else if (views[0] == "views[2]") {
					viewsdata[2] = views[1];
					if (topValues) {
						topValuesdata[2] = topValues[1];
					}
				}
				this.viewsdata = viewsdata;
				if (topValues) {
					this.topValuesdata = topValuesdata
				}
				// 保存当前索引到实例
				calendar.index = index;

				// 标记视图已经发送变化
				calendar.changed = true;
			},
			/**
			 * swiper 滑动结束
			 */
			swiperFinish(evt) {
				let calendar = this.calendar,
					t = this,
					arr0 = evt.currentTarget.dataset.data[0],
					arr1 = evt.currentTarget.dataset.data[1],
					arr2 = evt.currentTarget.dataset.data[2],
					arr = [];
				// 当视图发送变化，才会执行
				if (calendar.changed) {
					// console.log(calendar, 188)
					// 滑动结束后，自动设置日期
					if (evt.detail.source == "touch") {
						this.setDate(this.viewType == "week" ? calendar.currentWeek : "auto");
						let arrdata = '' + sswitch(calendar.currentMonth + '-' + calendar.currentDay);
						this.$emit('queryindex', arrdata)
					}
					// 重置标记
					calendar.changed = false;
				}
			},
			// 切换的过渡完成后，是否隐藏边缘
			// 当为周视图的时候，边缘可能会漏出其他日期的样式
			transitionEnd() {
				// this.hideEdge = this.viewType == "week" ? "hide-edge" : ""
			},
			// 点击选择某个日期
			selectDate(id, item) {
				console.log(id, item, '24555555')
				let _this = this;
				item.date = item.year + '-' + _this.addTimes(item.month) + '-' + _this.addTimes(item.day);
				this.$emit('returnData', item)
				// let dayrq = item.dayrq,
				// 	dataobj = '';
				// let data = {},
				// 	t = this,
				// 	date = item.date,
				// 	calendar = this.calendar,
				// 	isWeekView = this.viewType = "month";
				// // let daylidh = true;
				// // onfire.fire('daylidh', daylidh);
				// this.qujuassd = item;
				// let arrdata = '' + sswitch(date)
				// this.$emit('queryindex',arrdata)
				// // uni.$emit('queryindex', arrdata)
				// let month = ''
				// // 发送年月日
				// if (item.type != "current") {
				// 	// 如果选择的不是当前视图月份的日期，那么就切换
				// 	// 点击上一个月或者下一个月的日期，不会产生动画过渡，但是数据会变化
				// 	if (isWeekView) {
				// 		data = calendar.refreshWeekViews(date);
				// 		data.animate = "";
				// 	} else {
				// 		// 切换视图位置，点击上一个月或者下一个月的日期，会产生过渡变化
				// 		data.currentIndex = calendar.SIBLING[calendar.index][item.type];
				// 	}
				// 	this.currentIndex = data.currentIndex
				// 	// this.setData(data);
				// } else {

				// }

				// // 周视图下选择日期，要同时更新下一个星期和上一个星期的值
				// if (isWeekView) {
				// 	calendar.prevWeek = calendar.getDiffDate(date, -7);
				// 	calendar.nextWeek = calendar.getDiffDate(date, 7);
				// }

				// // 保存当前选择的 “日”
				// calendar.currentDay = date.split("-")[2];

				// this.setDate(date);
			},

			/**
			 * 设置激活日期状态，上传事件
			 */
			setDate(date) {
				if (date == "auto") {
					let calendar = this.calendar;

					// 自动灵活设置日期
					// 比如现在选择的是31号，切换到2月份，会自动选择 28 或者 29，因为2月份没有31号
					if (calendar.currentDay > calendar.currentView.totalDay) {
						calendar.currentDay = calendar.currentView.totalDay;

					}
					date = calendar.currentMonth + "-" + calendar.currentDay;

				}
				// console.log(date, 299)
				this.currentDate = date

				if (this.calendar.watchValue) {
					// 上传 change 事件
					this.value = date
					// this.triggerEvent("change", {
					// 	value: date
					// })
				}
			},
			/**
			 * 监听 value 值变化
			 */
			watchValue(date) {
				let calendar = this.calendar,
					value = calendar.getValue(date);
				// 根据视图类型使用不同的刷新机制
				// this.viewType == "month" ?
				// 	calendar.refreshMonthViews(value.month) :
				// 	calendar.refreshWeekViews(value.date)
				let valueData = this.viewType == "month" ?
					calendar.refreshMonthViews(value.month) :
					calendar.refreshWeekViews(value.date);
				if (this.viewType == "month") {
					this.topValues = [0, 0, 0]
				} else {
					this.topValues = valueData.topValues;
				}
				this.views = valueData.views;
				// console.log(valueData,344)
				this.setDate("auto");
			},
			/**
			 * 自定义手势事件+点击事件
			 */
			suoxiao(evta) {
				let burre = this.qujuassd.type;
				if (burre === "current") {
					let setData = {},
						data = this,
						trigger = false,
						calendar = this.calendar;
					this.animate = "animate";
					this.topValues = calendar.refreshWeekViews(data.currentDate).topValues;
					this.viewType = "week";
					this.views = calendar.refreshWeekViews(data.currentDate).views;
					// this.viewType = setData.viewType
				}
			},
			...WxTouch("Touch", {

				swipeVelocity: false,

				pressmove(evt) {
					this.calendar.pressmoved = true;
				},

				swipe(evt) {
					// let setData = {},
					// 	data = this,
					// 	trigger = false,
					// 	calendar = this.calendar,
					// 	thisd = this;
					// if (calendar.pressmoved) {
					// 	// 向上滑动更新为 “周” 视图，向下为 “月” 视图
					// 	// if (evt.direction == "up" && data.viewType == "month") {
					// 	// console.log('执行了月周切换', data.currentDate, calendar.refreshWeekViews(data.currentDate))
					// 	this.views = calendar.refreshWeekViews(data.currentDate).views;
					// 	this.topValues = calendar.refreshWeekViews(data.currentDate).topValues
					// 	data.viewType = "week";
					// 	trigger = true;
					// 	// } else if (evt.direction == "down" && data.viewType == "week") {
					// 	// 	if (thisd.ltop < 50) {
					// 	// 		this.views = calendar.refreshMonthViews(calendar.currentMonth).views
					// 	// 		// setData = calendar.refreshMonthViews(calendar.currentMonth);
					// 	// 		data.viewType = "month";
					// 	// 		this.topValues = [0, 0, 0];
					// 	// 		trigger = true;
					// 	// 	}
					// 	// }
					// 	this.animate = "animate";
					// 	// this.setDate(setData);
					// 	// 切换视图后上传事件
					// 	if (trigger) {
					// 		this.viewType = data.viewType;
					// 		// this.triggerEvent("viewchange", {
					// 		// 	viewType: setData.viewType
					// 		// })
					// 	}

					// 	calendar.pressmoved = false;
					// }
				}
			}),

		},
	};
</script>

<style>
	@import url("./index.css");

	.louChoose {
		background-color: #F56767;
		border-radius: 50%;
		color: #FFFFFF;
		/* height: 95rpx; */
	}
</style>
